package com.today.usercenter.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;

import com.today.lib.common.account.Account;
import com.today.lib.common.account.LoginInterface;
import com.today.lib.common.network.entity.LoginBaseRequestEntity;
import com.today.lib.common.utils.ToastUtils;
import com.today.usercenter.bus.events.LoginEvent;
import com.today.usercenter.listener.OnUserLoginListener;
import com.today.usercenter.network.RetrofitHelper;
import com.today.usercenter.network.entity.RegisterResponseEntity;
import com.today.usercenter.ui.activities.LoginActivity;

import org.greenrobot.eventbus.EventBus;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;


/**
 * Created by cgm on 18-6-19.
 */

public class LoginUtils implements LoginInterface {
    private OnUserLoginListener mOnUserLoginListener;

    public void setOnUserLoginListener(OnUserLoginListener listener) {
        mOnUserLoginListener = listener;
    }

    public LoginUtils() {
    }

    private void saveUserInfo(RegisterResponseEntity registerResponseEntity) {
        if (registerResponseEntity.userinfo == null)
            return;

        Account account = Account.get();

        account.mToken = registerResponseEntity.token;
        account.copy(registerResponseEntity.userinfo);
        account.save();
    }


    @SuppressLint("CheckResult")
    public void login(LoginBaseRequestEntity login_rqst) {
        Observable<RegisterResponseEntity> observable;
        observable = RetrofitHelper.getUserApi().login(login_rqst.sex, login_rqst.data);
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(registerResponseEntity -> {
                    if (registerResponseEntity.busCode == RegisterResponseEntity.STATUS_OK) {
                        saveUserInfo(registerResponseEntity);

                        if (mOnUserLoginListener != null) {
                            mOnUserLoginListener.onSuccess();
                            mOnUserLoginListener = null;
                        }

                        EventBus.getDefault().post(new LoginEvent(LoginEvent.STATUS_LOGIN));
                    } else {
                        ToastUtils.showToast(registerResponseEntity.busMsg);
                        if (mOnUserLoginListener != null) {
                            mOnUserLoginListener.onFailed();
                            mOnUserLoginListener = null;
                        }
                    }
                }, throwable -> {
                    throwable.printStackTrace();
                    if (mOnUserLoginListener != null) {
                        mOnUserLoginListener.onFailed();
                        mOnUserLoginListener = null;
                    }
                });
    }

    @Override
    public boolean checkLoginState(Context context) {
        if (Account.get().isLogin())
            return true;

        ToastUtils.showToast("请先登录");
        context.startActivity(new Intent(context, LoginActivity.class));
        return false;
    }
}
